package com.glsc.ngateway.flowable.listener.createproductnew;

import com.glsc.ngateway.common.api.flowable.dto.Result;
import com.glsc.ngateway.flowable.service.FlowableUserService;
import com.glsc.ngateway.flowable.service.feign.IEurekaAmproductFeignService;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.service.delegate.DelegateTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.List;

@Component
public class CreateProductNewStartHandler implements TaskListener {

    private static final Logger logger = LoggerFactory.getLogger(CreateProductNewStartHandler.class);

    public static CreateProductNewStartHandler createProductNewStartHandler;
    @Autowired
    private FlowableUserService flowableUserService;
    @Autowired
    IEurekaAmproductFeignService amproductFeignService;

    @PostConstruct
    public void init() {
        createProductNewStartHandler = this;
        createProductNewStartHandler.flowableUserService = this.flowableUserService;
        createProductNewStartHandler.amproductFeignService = this.amproductFeignService;
    }


    @Override
    public void notify(DelegateTask delegateTask) {

        try {

            //delegateTask.setAssignee("老板");
            String userid = delegateTask.getVariable("userid").toString();
            String content = delegateTask.getVariable("content").toString();

//        String department = createProductStartHandler.flowableUserService.getDepartment(userid);
//        delegateTask.setVariable("teamleader", createProductStartHandler.flowableUserService.getLeader(userid));
//        delegateTask.setVariable("manager", createProductStartHandler.flowableUserService.getManager(department));
//
//        JSONObject json = JSONObject.parseObject(content);
//        delegateTask.setVariable("days", json.getString("days"));

            delegateTask.setVariable("teamleader", "zy");
            delegateTask.setVariable("zgLeader", "zy");

//            delegateTask.setVariable("qdManager", "lm");

            //基础信息维护
            Result<List<String>> pmGroup =
                    createProductNewStartHandler.amproductFeignService.findAmProductUserIdList("amprod.role.产品经理");
            if (pmGroup.getCode() == 10000) {
                delegateTask.setVariable("pmGroup", pmGroup.getData());
                delegateTask.setVariable("pmGroupSize", pmGroup.getData().size());
            } else {
                throw new Exception("查询amprod.role.产品经理错误。");
            }

            //前一步指定  基础信息复核
//            delegateTask.setVariable("otherPmGroup", Arrays.asList(pmGroup));
//            delegateTask.setVariable("otherPmGroupSize", pmGroup.length);

            //运管TA资金
            Result<List<String>> taGroup =
                    createProductNewStartHandler.amproductFeignService.findAmProductUserIdList("amprod.role.运管TA组");
            if (taGroup.getCode() == 10000) {
                delegateTask.setVariable("taGroup", taGroup.getData());
                delegateTask.setVariable("taGroupSize", taGroup.getData().size());
            } else {
                throw new Exception("查询amprod.role.运管TA组。");
            }

            //估值
            Result<List<String>> gzGroup =
                    createProductNewStartHandler.amproductFeignService.findAmProductUserIdList("amprod.role.估值资金岗");
            if (gzGroup.getCode() == 10000) {
                delegateTask.setVariable("gzGroup", gzGroup.getData());
                delegateTask.setVariable("gzGroupSize", gzGroup.getData().size());
            } else {
                throw new Exception("查询amprod.role.估值资金岗。");
            }

            //信息披露
            Result<List<String>> xpGroup =
                    createProductNewStartHandler.amproductFeignService.findAmProductUserIdList("amprod.role.信息披露岗");
            if (xpGroup.getCode() == 10000) {
                delegateTask.setVariable("xpGroup", xpGroup.getData());
                delegateTask.setVariable("xpGroupSize", xpGroup.getData().size());
            } else {
                throw new Exception("查询amprod.role.信息披露岗。");
            }

            //TODO
            String[] dzhtGroup = {"gjc1", "gjc2", "gjc3"};
            delegateTask.setVariable("dzhtGroup", Arrays.asList(dzhtGroup));
            delegateTask.setVariable("dzhtGroupSize", dzhtGroup.length);

            //出入金用监听
//            String[] crjGroup = {"lx1", "lx2", "lx3"};
//            delegateTask.setVariable("crjGroup", Arrays.asList(crjGroup));
//            delegateTask.setVariable("crjGroupSize", crjGroup.length);


            //资管风控岗
            Result<List<String>> fxGroup =
                    createProductNewStartHandler.amproductFeignService.findAmProductUserIdList("amprod.role.资管风控岗");
            if (fxGroup.getCode() == 10000) {
                delegateTask.setVariable("fxGroup", fxGroup.getData());
                delegateTask.setVariable("fxGroupSize", fxGroup.getData().size());
            } else {
                throw new Exception("查询amprod.role.信息披露岗。");
            }


        } catch (Exception e) {
            logger.error("CreateProductNewStartHandler处理异常，结束处理。processid=" + delegateTask.getProcessInstanceId(), e);
            throw new RuntimeException("CreateProductNewStartHandler处理异常，结束处理。processid=" + delegateTask.getProcessInstanceId(), e);
        }
    }

}